home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swag9502.zip / XX3402.ZIP / CRCASM.ZIP / TESTCRC.PAS < prev   
Pascal/Delphi Source File  |  1990-10-30  |  3KB  |  82 lines

  1.  
  2. {$A+,B-,D-,E-,F-,I+,L-,N-,O-,R-,S-,V+}
  3.  
  4. {$M 8192,0,0}
  5.  
  6. { TESTCRC - Simple test program for crc routines.  This program opens the
  7.   file specified in the first command line parameter, computes three
  8.   kinds of CRC, and writes them to stdout in hex.  Then it checks the
  9.   peculiar behavior of the XModem CRC.  E. Floyd [76067,747], 10-29-89. }
  10.  
  11. program TestCRC;
  12. uses CRC;
  13. const
  14.   BufSize = 32768;
  15. type
  16.   Str2 = string[2];
  17.   Str4 = string[4];
  18.   Str8 = string[8];
  19. var
  20.   Crc32 : longint;
  21.   InFile : file;
  22.   InBuf : array[1..BufSize] of byte;
  23.   Len, Crc16, CrcArc, SaveCrc : word;
  24.  
  25.   function HexByte(b : byte) : Str2;
  26.   const
  27.     Hex : array[$0..$F] of char = '0123456789abcdef';
  28.   begin
  29.     HexByte := Hex[b shr 4] + Hex[b and $F];
  30.   end;
  31.  
  32.   function HexWord(w : word) : Str4;
  33.   begin
  34.     HexWord := HexByte(hi(w)) + HexByte(lo(w));
  35.   end;
  36.  
  37.   function HexLong(ww : longint) : Str8;
  38.   var
  39.     w : array[1..2] of word absolute ww;
  40.   begin
  41.     HexLong := HexWord(w[2]) + HexWord(w[1]);
  42.   end;
  43.  
  44. BEGIN
  45.   if paramcount < 1 then
  46.     begin
  47.       writeln('Run like: TESTCRC <filename>');
  48.       writeln('Prints crc16, CrcArc and crc32 in hex');
  49.     end
  50.   else
  51.     begin
  52.       {$I-}
  53.       assign(InFile, paramstr(1));
  54.       reset(InFile, 1);
  55.       {$I+}
  56.       if ioresult = 0 then begin
  57.         Crc16 := 0;                    { "XModem" crc starts with zero.. }
  58.         CrcArc := 0;                   { ..as does ARC crc }
  59.         Crc32 := $FFFFFFFF;            { 32 bit crc starts with all bits on }
  60.         repeat
  61.           blockread(InFile, InBuf, BufSize, Len);
  62.           Crc16 := UpdateCrc16(Crc16, InBuf, Len);
  63.           CrcArc := UpdateCrcArc(CrcArc, InBuf, Len);
  64.           Crc32 := UpdateCrc32(Crc32, InBuf, Len);
  65.         until eof(InFile);
  66.         close(InFile);
  67.         SaveCrc := Crc16;              { Save near-complete XModem crc for test below }
  68.         fillchar(InBuf, 2, 0);         { Finish XModem crc with two nulls }
  69.         Crc16 := UpdateCrc16(Crc16, InBuf, 2);
  70.         Crc32 := not(Crc32);           { Finish 32 bit crc by inverting all bits }
  71.         writeln('Crc16 = ', HexWord(Crc16), ', CrcArc = ', HexWord(CrcArc),
  72.                 ', Crc32 = ', HexLong(Crc32));
  73.         { Now test for XModem crc trick - update the near-complete crc with.. }
  74.         Crc16 := swap(Crc16);          { ..the complete crc in hi:lo order in memory. }
  75.         writeln('XModem crc test = ', HexWord(UpdateCrc16(SaveCrc, Crc16, 2)));
  76.         { The result should always be zero }
  77.       end
  78.     else
  79.       writeln('Unable to open file ', paramstr(1));
  80.     end;
  81. END.
  82.